package com.haha.community.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.text.html.HTML;

@Component
public class MailClient {
    //委托给163邮箱进行发送，对spring提供的发送邮件进行简单的封装
    //声明一个记录日志的类
    private static Logger logger = LoggerFactory.getLogger(MailClient.class);
    //注入核心的组件
    @Autowired
     private  JavaMailSender javaMailSender;
    //注入发件人
    @Value("${spring.mail.username}")
    private  String username;
    //提供发送邮件接口
    public  void sendEmail(String to,String title,String content){
        try {
            //进行发送邮件相关的逻辑
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
            messageHelper.setFrom(username);
            messageHelper.setTo(to);
            messageHelper.setSubject(title);
            messageHelper.setText(content, true);
            javaMailSender.send(messageHelper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件错误"+e.getMessage());
        }


    }


}
